package com.qdxht.dispenser.utils;

import android.content.Context;

public class UnitUtil {
	/**
	 * dp转px
	 * @param context
	 * @param dipValue
	 * @return
	 */
	public static float dip2px(Context context, float dipValue) {
		float scale = context.getResources().getDisplayMetrics().density;
//		MLog.i("density", "density"+scale);
		return dipValue * scale + 0.5f;
	}

	/**
	 * px转dp
	 * @param context
	 * @param pxValue
	 * @return
	 */
	public static float px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
//		MLog.i("density", "density"+scale);
		return pxValue / scale + 0.5f;
	}

	/**
	 * 将px值转换为sp值
	 * @param pxValue
	 * @return
	 */
	public static int px2sp(Context context, float pxValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}

	/**
	 * 将sp值转换为px值
	 * @param spValue
	 * @return
	 */
	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}
}
